#include "LoginWidget.h"

#include <qlabel.h>
#include <qlineedit.h>
#include <qsettings.h>
#include <qpushbutton.h>
#include <qcheckbox.h>
#include <qlayout.h>


LoginWidget::LoginWidget(QWidget * parent, const char * name)
	: QWidget(parent, name)
{
	setPaletteBackgroundColor(parent ? parent->paletteBackgroundColor() : Qt::white);

	QFont font("Arial", 9);
	setFont(font);

	_pLabelUsername = new QLabel(this);
	_pLabelUsername->setPaletteBackgroundColor(paletteBackgroundColor());
	_pLabelUsername->setText(tr("Username:"));

	_pLineEditUsername = new QLineEdit(this);
	_pLineEditUsername->setLineWidth(1);
	_pLineEditUsername->setFrameStyle(QFrame::Plain | QFrame::Box);

	QVBoxLayout * vlayoutUsername = new QVBoxLayout	(0, 0, 3);
	vlayoutUsername->addWidget(_pLabelUsername);
	vlayoutUsername->addWidget(_pLineEditUsername);


	_pLabelPassword = new QLabel(this);
	_pLabelPassword->setPaletteBackgroundColor(paletteBackgroundColor());
	_pLabelPassword->setText(tr("Password:"));

	_pLineEditPassword = new QLineEdit(this);
	_pLineEditPassword->setEchoMode(QLineEdit::Password);
	_pLineEditPassword->setLineWidth(1);
	_pLineEditPassword->setFrameStyle(QFrame::Plain | QFrame::Box);

	QVBoxLayout * vlayoutPassword = new QVBoxLayout(0, 0, 3);
	vlayoutPassword->addWidget(_pLabelPassword);
	vlayoutPassword->addWidget(_pLineEditPassword);

	
	_pCheckBoxRememberPassword = new QCheckBox(this);
	_pCheckBoxRememberPassword->setText(tr("Remember password"));
	_pCheckBoxRememberPassword->setPaletteBackgroundColor(paletteBackgroundColor());


	pPushButtonSignIn = new QPushButton(this);
	pPushButtonSignIn->setText(tr("Sign In"));
	QHBoxLayout * hlayoutSignIn = new QHBoxLayout(0, 0, 0);
	hlayoutSignIn->addStretch();
	hlayoutSignIn->addWidget(pPushButtonSignIn);
	hlayoutSignIn->addStretch();

	QFont fontLabel(this->font());
	fontLabel.setUnderline(true);

	QLabel * pLabelForgot = new QLabel(this);
	pLabelForgot->setText(tr("Forgot yout password?"));
	pLabelForgot->setPaletteForegroundColor(QColor(52, 83, 116));
	pLabelForgot->setPaletteBackgroundColor(paletteBackgroundColor());
	pLabelForgot->setFont(fontLabel);
	pLabelForgot->setAlignment(Qt::AlignHCenter);

	QLabel * pLabelHaveAccount = new QLabel(this);
	pLabelHaveAccount->setText(tr("Don't have an account?"));
	pLabelHaveAccount->setPaletteForegroundColor(QColor(52, 83, 116));
	pLabelHaveAccount->setPaletteBackgroundColor(paletteBackgroundColor());
	pLabelHaveAccount->setFont(fontLabel);
	pLabelHaveAccount->setAlignment(Qt::AlignHCenter);		


	QVBoxLayout * vlayoutForm = new QVBoxLayout;
	vlayoutForm->addStretch();
	vlayoutForm->addLayout(vlayoutUsername);
	vlayoutForm->addLayout(vlayoutPassword);
	vlayoutForm->addWidget(_pCheckBoxRememberPassword);
	vlayoutForm->addLayout(hlayoutSignIn);
	vlayoutForm->addWidget(pLabelForgot);
	vlayoutForm->addWidget(pLabelHaveAccount);
	vlayoutForm->addStretch();	


	QHBoxLayout * hlayoutMain = new QHBoxLayout(this, 24, 11);
	hlayoutMain->addStretch();
	hlayoutMain->addLayout(vlayoutForm);
	hlayoutMain->addStretch();
}

LoginWidget::~LoginWidget()
{
	qDebug("~LoginWidget()");
}

void LoginWidget::setPassword(const QString & sPassword)
{
	_pLineEditPassword->setText(sPassword);
}

QString LoginWidget::password()
{
	return _pLineEditPassword->text();
}

void LoginWidget::setUsername(const QString & sUsername)
{
	_pLineEditUsername->setText(sUsername);
}

QString LoginWidget::username()
{
	return _pLineEditUsername->text();
}

void LoginWidget::setRemember(bool b)
{
	_pCheckBoxRememberPassword->setChecked(b);
}

bool LoginWidget::remember()
{
	return _pCheckBoxRememberPassword->isChecked();
}

void LoginWidget::enableControls(bool bEnable)
{
	_pLabelUsername->setEnabled(bEnable);
	_pLineEditUsername->setEnabled(bEnable);

	_pLabelPassword->setEnabled(bEnable);
	_pLineEditPassword->setEnabled(bEnable);

	pPushButtonSignIn->setEnabled(bEnable);
}
